﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using KPIS.DBM;
using KPIS.GERP.GHRMS.MODEL;

namespace KPIS.GERP.GHRMS.DAL
{
    public sealed class PersonalDAL
    {
        IDBManager dbManager = new DBManager(SystemInfo._DataProvider, SystemInfo._ConnectionString);

        public PersonalDAL()
        {

        }
        public PersonalInfo GetPersonalInfo(Nullable<int> seq)
        {
            string strSQL;
            PersonalInfo posonalInfo = new PersonalInfo();

            try
            {
                dbManager.Open();
                dbManager.CreateParameters(2);

                dbManager.AddParameters(0, "@seq", seq);
                dbManager.AddParameters(1, "@DeleteRecord", SystemInfo._DeleteRecordStatus);

                strSQL = "SELECT p.PERSON_SEQ, p.NAME_PREFIX_SEQ, p.FIRST_NAME, p.LAST_NAME"
                          + ", p.FIRST_NAME_ENG, p.LAST_NAME_ENG, p.GENDER_SEQ, p.RELIGION_SEQ"
                          + ", p.RACE_NAT_SEQ, p.NAT_SEQ, p.BIRTH_DATE, p.PERSON_STATUS_SEQ"
                          + ", p.CITIZEN, p.TAX_PAYER, p.SOCIAL, p.OFFICIAL, p.BLOOD_GROUP, p.REMARKS"
                     + " FROM HRM_RC_PERSONALS p"
                     + " WHERE p.RECORD_STATUS not like @DeleteRecord"
                          + " AND p.PERSON_SEQ = @seq";

                IDataReader rdr = dbManager.ExecuteReader(CommandType.Text, strSQL);

                while (rdr.Read())
                {
                    posonalInfo.PersonSeq = rdr.IsDBNull(0) ? (int?)null : rdr.GetInt32(0);
                    posonalInfo.NamePrefixSeq = rdr.IsDBNull(1) ? (int?)null : rdr.GetInt32(1);
                    posonalInfo.FirstName = rdr.IsDBNull(2) ? "" : rdr.GetString(2);
                    posonalInfo.LastName = rdr.IsDBNull(3) ? "" : rdr.GetString(3);
                    posonalInfo.FirstNameEng = rdr.IsDBNull(4) ? "" : rdr.GetString(4);
                    posonalInfo.LastNameEng = rdr.IsDBNull(5) ? "" : rdr.GetString(5);
                    posonalInfo.GenderSeq = rdr.IsDBNull(6) ? (int?)null : rdr.GetInt32(6);
                    posonalInfo.Religion = rdr.IsDBNull(7) ? (int?)null : rdr.GetInt32(7);
                    posonalInfo.RaceNational = rdr.IsDBNull(8) ? (int?)null : rdr.GetInt32(8);
                    posonalInfo.National = rdr.IsDBNull(9) ? (int?)null : rdr.GetInt32(9);
                    posonalInfo.BirthDate = rdr.IsDBNull(10) ? (DateTime?)null : rdr.GetDateTime(10);
                    posonalInfo.PersonalStatus = rdr.IsDBNull(11) ? (int?)null : rdr.GetInt32(11);
                    posonalInfo.Citizen = rdr.IsDBNull(12) ? "" : rdr.GetString(12);
                    posonalInfo.TaxPayer = rdr.IsDBNull(13) ? "" : rdr.GetString(13);
                    posonalInfo.Social = rdr.IsDBNull(14) ? "" : rdr.GetString(14);
                    posonalInfo.Official = rdr.IsDBNull(15) ? "" : rdr.GetString(15);
                    posonalInfo.BloodGroup = rdr.IsDBNull(16) ? "" : rdr.GetString(16);
                    posonalInfo.Remarks = rdr.IsDBNull(17) ? "" : rdr.GetString(17);
                }

                return posonalInfo;
            }
            catch
            {
                return null;
                throw;
            }
            finally
            {
     
                dbManager.Dispose();
            }
        }
        public DataSet LoadGridPersonals()
        {
            string strSQL;

            try
            {
                dbManager.Open();
                dbManager.CreateParameters(1);

                dbManager.AddParameters(0, "@DeleteRecord", SystemInfo._DeleteRecordStatus);

                strSQL = "SELECT p.PERSON_SEQ, p.NAME_PREFIX_SEQ, p.FIRST_NAME, p.LAST_NAME"
                          + ", p.FIRST_NAME_ENG, p.LAST_NAME_ENG, p.GENDER_SEQ, p.RELIGION_SEQ"
                          + ", p.RACE_NAT_SEQ, p.NAT_SEQ, p.BIRTH_DATE, p.PERSON_STATUS_SEQ"
                          + ", p.CITIZEN, p.TAX_PAYER, p.SOCIAL, p.OFFICIAL, p.BLOOD_GROUP, p.REMARKS"
                          + ", CONCAT(np.PREFIX_NAME, p.FIRST_NAME, ' ', p.LAST_NAME) AS PERSON_NAME"
                     + " FROM HRM_RC_PERSONALS p"
                        + " LEFT JOIN MAS_NAME_PREFIXES np ON p.NAME_PREFIX_SEQ = np.NAME_PREFIX_SEQ"
                     + " WHERE p.RECORD_STATUS not like @DeleteRecord";

                return dbManager.ExecuteDataSet(CommandType.Text, strSQL);
            }
            catch
            {
                return null;
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }
        public DataSet LoadListPersonals()
        {
            string strSQL;

            try
            {
                dbManager.Open();
                dbManager.CreateParameters(1);

                dbManager.AddParameters(0, "@DeleteRecord", SystemInfo._DeleteRecordStatus);

                strSQL = "SELECT p.PERSON_SEQ"
                        + ", CONCAT(np.PREFIX_NAME, p.FIRST_NAME, ' ', p.LAST_NAME) AS PERSON_NAME"
                        + ", CONCAT(np.PREFIX_NAME_ENG, p.FIRST_NAME_ENG, ' ', p.LAST_NAME_ENG) AS PERSON_NAME_ENG"
                     + " FROM HRM_RC_PERSONALS p"
                        + " LEFT JOIN MAS_NAME_PREFIXES np ON p.NAME_PREFIX_SEQ = np.NAME_PREFIX_SEQ"
                     + " WHERE p.RECORD_STATUS not like @DeleteRecord";

                return dbManager.ExecuteDataSet(CommandType.Text, strSQL);
            }
            catch
            {
                return null;
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }

        public void Insert(PersonalInfo personInfo)
        {
            string strSQL;

            try
            {
                dbManager.Open();
                dbManager.CreateParameters(20);

                dbManager.AddParameters(0, "@NamePrefixSeq", personInfo.NamePrefixSeq);
                dbManager.AddParameters(1, "@FirstName", personInfo.FirstName);
                dbManager.AddParameters(2, "@LastName", personInfo.LastName);
                dbManager.AddParameters(3, "@FirstNameEng", personInfo.FirstNameEng);
                dbManager.AddParameters(4, "@LastNameEng", personInfo.LastNameEng);
                dbManager.AddParameters(5, "@GenderSeq", personInfo.GenderSeq);
                dbManager.AddParameters(6, "@ReligionSeq", personInfo.Religion);
                dbManager.AddParameters(7, "@RaceNational", personInfo.RaceNational);
                dbManager.AddParameters(8, "@National", personInfo.National);
                dbManager.AddParameters(9, "@BirthDate", personInfo.BirthDate);
                dbManager.AddParameters(10, "@PersonStatusSeq", personInfo.PersonalStatus);
                dbManager.AddParameters(11, "@Citizen", personInfo.Citizen);
                dbManager.AddParameters(12, "@TaxPayer", personInfo.TaxPayer);
                dbManager.AddParameters(13, "@Social", personInfo.Social);
                dbManager.AddParameters(14, "@Official", personInfo.Official);
                dbManager.AddParameters(15, "@BloodGroup", personInfo.BloodGroup);
                dbManager.AddParameters(16, "@Remarks", personInfo.Remarks);
                dbManager.AddParameters(17, "@RecordStatus", SystemInfo._ActiveRecordStatus);
                dbManager.AddParameters(18, "@CreatedBy", personInfo.CreatedBy);
                dbManager.AddParameters(19, "@CreatedWhen", DateTime.Today);

                strSQL = "INSERT INTO HRM_RC_PERSONALS(NAME_PREFIX_SEQ, FIRST_NAME, LAST_NAME"
                          + ", FIRST_NAME_ENG, LAST_NAME_ENG, GENDER_SEQ, RELIGION_SEQ, RACE_NAT_SEQ"
                          + ", NAT_SEQ, BIRTH_DATE, PERSON_STATUS_SEQ, CITIZEN, TAX_PAYER, SOCIAL"
                          + ", OFFICIAL, BLOOD_GROUP, REMARKS, RECORD_STATUS, CREATED_BY, CREATED_WHEN)"
                    + " VALUES(@NamePrefixSeq, @FirstName, @LastName, @FirstNameEng, @LastNameEng, @GenderSeq"
                          + ", @ReligionSeq, @RaceNational, @National, @BirthDate, @PersonStatusSeq, @Citizen"
                          + ", @TaxPayer, @Social, @Official, @BloodGroup, @Remarks, @RecordStatus, @CreatedBy, @CreatedWhen)";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
            }
            catch
            {
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }
        public void Update(PersonalInfo personInfo)
        {
            string strSQL;

            try
            {
                dbManager.Open();
                dbManager.CreateParameters(20);

                dbManager.AddParameters(0, "@Seq", personInfo.PersonSeq);
                dbManager.AddParameters(1, "@NamePrefixSeq", personInfo.NamePrefixSeq);
                dbManager.AddParameters(2, "@FirstName", personInfo.FirstName);
                dbManager.AddParameters(3, "@LastName", personInfo.LastName);
                dbManager.AddParameters(4, "@FirstNameEng", personInfo.FirstNameEng);
                dbManager.AddParameters(5, "@LastNameEng", personInfo.LastNameEng);
                dbManager.AddParameters(6, "@GenderSeq", personInfo.GenderSeq);
                dbManager.AddParameters(7, "@ReligionSeq", personInfo.Religion);
                dbManager.AddParameters(8, "@RaceNational", personInfo.RaceNational);
                dbManager.AddParameters(9, "@National", personInfo.National);
                dbManager.AddParameters(10, "@BirthDate", personInfo.BirthDate);
                dbManager.AddParameters(11, "@PersonStatusSeq", personInfo.PersonalStatus);
                dbManager.AddParameters(12, "@Citizen", personInfo.Citizen);
                dbManager.AddParameters(13, "@TaxPayer", personInfo.TaxPayer);
                dbManager.AddParameters(14, "@Social", personInfo.Social);
                dbManager.AddParameters(15, "@Official", personInfo.Official);
                dbManager.AddParameters(16, "@BloodGroup", personInfo.BloodGroup);
                dbManager.AddParameters(17, "@Remarks", personInfo.Remarks);
                dbManager.AddParameters(18, "@UpdatedBy", personInfo.UpdatedBy);
                dbManager.AddParameters(19, "@UpdatedWhen", DateTime.Today);

                strSQL = "UPDATE HRM_RC_PERSONALS"
                            + " SET NAME_PREFIX_SEQ = @NamePrefixSeq"
                            + ", FIRST_NAME = @FirstName"
                            + ", LAST_NAME = @LastName"
                            + ", FIRST_NAME_ENG = @FirstNameEng"
                            + ", LAST_NAME_ENG = @LastNameEng"
                            + ", GENDER_SEQ = @GenderSeq"
                            + ", RELIGION_SEQ = @ReligionSeq"
                            + ", RACE_NAT_SEQ = @RaceNational"
                            + ", NAT_SEQ = @National"
                            + ", BIRTH_DATE = @BirthDate"
                            + ", PERSON_STATUS_SEQ = @PersonStatusSeq"
                            + ", CITIZEN = @Citizen"
                            + ", TAX_PAYER = @TaxPayer"
                            + ", SOCIAL = @Social"
                            + ", OFFICIAL = @Official"
                            + ", BLOOD_GROUP = @BloodGroup"
                            + ", REMARKS = @Remarks"
                            + ", UPDATED_BY = @UpdatedBy"
                            + ", UPDATED_WHEN = @UpdatedWhen"
                        + " WHERE PERSON_SEQ = @Seq";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
            }
            catch
            {
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }
        public void Delete(PersonalInfo personInfo)
        {
            string strSQL;

            try
            {
                dbManager.Open();
                dbManager.CreateParameters(4);

                dbManager.AddParameters(0, "@seq", personInfo.PersonSeq);
                dbManager.AddParameters(1, "@record_status", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(2, "@updated_by", personInfo.UpdatedBy);
                dbManager.AddParameters(3, "@updated_when", DateTime.Today);

                strSQL = "UPDATE HRM_RC_PERSONALS"
                    + " SET RECORD_STATUS = @record_status"
                        + ", UPDATED_BY = @updated_by"
                        + ", UPDATED_WHEN = @updated_when"
                    + " WHERE PERSON_SEQ = @seq";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
            }
            catch
            {
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }
        //public int Insert(Nullable<int> NamePrefixSeq, string FirstName, string LastName, string FirstNameEng
        //        , string LastNameEng, Nullable<int> GenderSeq, Nullable<int> ReligionSeq, Nullable<int> RaceNational
        //        , Nullable<int> National, Nullable<DateTime> BirthDate, Nullable<int> PersonStatusSeq, string Citizen
        //        , string TaxPayer, string Social, string Official, string BloodGroup, string Remarks, int CreatedBy)
        //{
        //    string strSQL;

        //    try
        //    {
        //        dbManager.Open();
        //        dbManager.CreateParameters(20);

        //        dbManager.AddParameters(0, "@NamePrefixSeq", NamePrefixSeq);
        //        dbManager.AddParameters(1, "@FirstName", FirstName);
        //        dbManager.AddParameters(2, "@LastName", LastName);
        //        dbManager.AddParameters(3, "@FirstNameEng", FirstNameEng);
        //        dbManager.AddParameters(4, "@LastNameEng", LastNameEng);
        //        dbManager.AddParameters(5, "@GenderSeq", GenderSeq);
        //        dbManager.AddParameters(6, "@ReligionSeq", ReligionSeq);
        //        dbManager.AddParameters(7, "@RaceNational", RaceNational);
        //        dbManager.AddParameters(8, "@National", National);
        //        dbManager.AddParameters(9, "@BirthDate", BirthDate);
        //        dbManager.AddParameters(10, "@PersonStatusSeq", PersonStatusSeq);
        //        dbManager.AddParameters(11, "@Citizen", Citizen);
        //        dbManager.AddParameters(12, "@TaxPayer", TaxPayer);
        //        dbManager.AddParameters(13, "@Social", Social);
        //        dbManager.AddParameters(14, "@Official", Official);
        //        dbManager.AddParameters(15, "@BloodGroup", BloodGroup);
        //        dbManager.AddParameters(16, "@Remarks", Remarks);
        //        dbManager.AddParameters(17, "@RecordStatus", SystemInfo._ActiveRecordStatus);
        //        dbManager.AddParameters(18, "@CreatedBy", CreatedBy);
        //        dbManager.AddParameters(19, "@CreatedWhen", DateTime.Today);

        //        strSQL = "INSERT INTO HRM_RC_PERSONALS(NAME_PREFIX_SEQ, FIRST_NAME, LAST_NAME"
        //                  + ", FIRST_NAME_ENG, LAST_NAME_ENG, GENDER_SEQ, RELIGION_SEQ, RACE_NAT_SEQ"
        //                  + ", NAT_SEQ, BIRTH_DATE, PERSON_STATUS_SEQ, CITIZEN, TAX_PAYER, SOCIAL"
        //                  + ", OFFICIAL, BLOOD_GROUP, REMARKS, RECORD_STATUS, CREATED_BY, CREATED_WHEN)"
        //            + " VALUES(@NamePrefixSeq, @FirstName, @LastName, @FirstNameEng, @LastNameEng, @GenderSeq"
        //                  + ", @ReligionSeq, @RaceNational, @National, @BirthDate, @PersonStatusSeq, @Citizen"
        //                  + ", @TaxPayer, @Social, @Official, @BloodGroup, @Remarks, @RecordStatus, @CreatedBy, @CreatedWhen)";

        //        return dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
        //    }
        //    catch
        //    {
        //        return 0;
        //        throw;
        //    }
        //    finally
        //    {
        //        dbManager.Dispose();
        //    }
        //}

        //public int Update(Nullable<int> Seq, Nullable<int> NamePrefixSeq, string FirstName, string LastName
        //        , string FirstNameEng, string LastNameEng, Nullable<int> GenderSeq, Nullable<int> ReligionSeq
        //        , Nullable<int> RaceNational, Nullable<int> National, Nullable<DateTime> BirthDate
        //        , Nullable<int> PersonStatusSeq, string Citizen, string TaxPayer, string Social, string Official
        //        , string BloodGroup, string Remarks, int UpdatedBy)
        //{
        //    string strSQL;

        //    try
        //    {
        //        dbManager.Open();
        //        dbManager.CreateParameters(20);

        //        dbManager.AddParameters(0, "@Seq", Seq);
        //        dbManager.AddParameters(1, "@NamePrefixSeq", NamePrefixSeq);
        //        dbManager.AddParameters(2, "@FirstName", FirstName);
        //        dbManager.AddParameters(3, "@LastName", LastName);
        //        dbManager.AddParameters(4, "@FirstNameEng", FirstNameEng);
        //        dbManager.AddParameters(5, "@LastNameEng", LastNameEng);
        //        dbManager.AddParameters(6, "@GenderSeq", GenderSeq);
        //        dbManager.AddParameters(7, "@ReligionSeq", ReligionSeq);
        //        dbManager.AddParameters(8, "@RaceNational", RaceNational);
        //        dbManager.AddParameters(9, "@National", National);
        //        dbManager.AddParameters(10, "@BirthDate", BirthDate);
        //        dbManager.AddParameters(11, "@PersonStatusSeq", PersonStatusSeq);
        //        dbManager.AddParameters(12, "@Citizen", Citizen);
        //        dbManager.AddParameters(13, "@TaxPayer", TaxPayer);
        //        dbManager.AddParameters(14, "@Social", Social);
        //        dbManager.AddParameters(15, "@Official", Official);
        //        dbManager.AddParameters(16, "@BloodGroup", BloodGroup);
        //        dbManager.AddParameters(17, "@Remarks", Remarks);
        //        dbManager.AddParameters(18, "@UpdatedBy", UpdatedBy);
        //        dbManager.AddParameters(19, "@UpdatedWhen", DateTime.Today);

        //        strSQL = "UPDATE HRM_RC_PERSONALS"
        //                    + " SET NAME_PREFIX_SEQ = @NamePrefixSeq"
        //                    + ", FIRST_NAME = @FirstName"
        //                    + ", LAST_NAME = @LastName"
        //                    + ", FIRST_NAME_ENG = @FirstNameEng"
        //                    + ", LAST_NAME_ENG = @LastNameEng"
        //                    + ", GENDER_SEQ = @GenderSeq"
        //                    + ", RELIGION_SEQ = @ReligionSeq"
        //                    + ", RACE_NAT_SEQ = @RaceNational"
        //                    + ", NAT_SEQ = @National"
        //                    + ", BIRTH_DATE = @BirthDate"
        //                    + ", PERSON_STATUS_SEQ = @PersonStatusSeq"
        //                    + ", CITIZEN = @Citizen"
        //                    + ", TAX_PAYER = @TaxPayer"
        //                    + ", SOCIAL = @Social"
        //                    + ", OFFICIAL = @Official"
        //                    + ", BLOOD_GROUP = @BloodGroup"
        //                    + ", REMARKS = @Remarks"
        //                    + ", RECORD_STATUS = @RecordStatus"
        //                    + ", UPDATED_BY = @UpdatedBy"
        //                    + ", UPDATED_WHEN = @UpdatedWhen"
        //                + " WHERE POS_TYPE_SEQ = @Seq";

        //        return dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
        //    }
        //    catch
        //    {
        //        return 0;
        //        throw;
        //    }
        //    finally
        //    {
        //        dbManager.Dispose();
        //    }
        //}

        //public int Delete(Nullable<int> seq, int updated_by)
        //{
        //    string strSQL;

        //    try
        //    {
        //        dbManager.Open();
        //        dbManager.CreateParameters(4);

        //        dbManager.AddParameters(0, "@seq", seq);
        //        dbManager.AddParameters(1, "@record_status", SystemInfo._DeleteRecordStatus);
        //        dbManager.AddParameters(2, "@updated_by", updated_by);
        //        dbManager.AddParameters(3, "@updated_when", DateTime.Today);

        //        strSQL = "UPDATE HRM_RC_PERSONALS"
        //            + " SET RECORD_STATUS = @record_status"
        //                + ", UPDATED_BY = @updated_by"
        //                + ", UPDATED_WHEN = @updated_when"
        //            + " WHERE PERSON_SEQ = @seq";

        //        return dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
        //    }
        //    catch
        //    {
        //        return 0;
        //        throw;
        //    }
        //    finally
        //    {
        //        dbManager.Dispose();
        //    }
        //}
    }
}
